# coding=utf-8
import codecs
import logging
import sys
import csv

import os

reload(sys);
sys.setdefaultencoding("utf-8");

out = open('D:/03.Documents/bxjg.csv', 'wb')
out.write(codecs.BOM_UTF8)
csv_write = csv.writer(out, dialect='excel')
header = [u'序号', u'被处罚公司', u'被处罚人', u'原文', u'采取的监管或强制措施', u'备注', u'来源路径']
csv_write.writerow(header)
ll = ['当事人：', "代表人：", "处罚人："]


def process(i, path):
    print path
    with codecs.open(path, 'r', encoding='utf-8') as f:
        row = [i, '', '', '', '', '', path]
        all_text = ''
        for line in f.readlines():
            print line
            t = line
            all_text += line + '\n'
            for p in ll:
                if p in t:
                    l = len(t.strip())
                    if len(t.strip()) > 28:
                        row[1] += (t + '\n')
                    else:
                        row[2] += (t + '\n')
            if "职位：" in t or "职务：" in t:
                row[2] += t
        row[3] = all_text
        csv_write.writerow(row)


def process_dir(path):
    rootdir = path
    list = os.listdir(unicode(rootdir, 'utf-8'))  # 列出文件夹下所有的目录与文件
    for i in range(0, len(list)):
        path = os.path.join(rootdir, list[i])
        if os.path.isfile(path):
            try:
                process(i, path)
            except BaseException, ex:
                logging.error(ex)


process_dir('D:/03.Documents/bxjg_file')

# process(1, 'd:/tet6.txt')
# rootdir = 'D:/projects/bxjg_1'
# list = os.listdir(unicode(rootdir, 'utf-8')) #列出文件夹下所有的目录与文件
# for i in range(0,len(list)):
#     path = os.path.join(rootdir,list[i])
#     if os.path.isfile(path):
#         try:
#             process(i, path)
#         except BaseException, ex:
#             logging.info(ex)


# e = d.text()
# print e
# # aaa = d('a');
# # for a in aaa:
# #     a.remove();
# # [a.remove() for a in aaa]
# tag_list = d('p').items()
# for tag in tag_list:
#     print(tag)
# plist = d('p').remove('a');
# index = 0
# row = ['', '', d.text(), '', '']
# for p in plist:
#     index = index + 1
#     row[0] = index
#     t = p.text
#     if "当事人：" in t:
#         row[1] += t
#     if "职位：" in t or "职务：" in t:
#         row[1] += t
# csv_write.writerow(row)
#
# print(len(plist))
